package com.chenwenxuan.sync.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

import com.javadocking.DockingManager;
import com.javadocking.dock.FloatDock;
import com.javadocking.dock.Position;
import com.javadocking.dock.SplitDock;
import com.javadocking.dock.TabDock;
import com.javadocking.dockable.DefaultDockable;
import com.javadocking.dockable.Dockable;
import com.javadocking.dockable.DockingMode;
import com.javadocking.model.FloatDockModel;

public class TestDock extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4622960846432174808L;

	/**
	 * Create the panel.
	 */
	public TestDock(JFrame frame) {
		super(new BorderLayout());

		// Create the split panes.
		JSplitPane leftSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		leftSplitPane.setDividerLocation(250);
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		splitPane.setDividerLocation(400);
		splitPane.setLeftComponent(leftSplitPane);

		// Add the split pane to the panel.
		add(splitPane, BorderLayout.CENTER);
		
		// Create the content components.
		TextPanel textPanel1 = new TextPanel("I am window 1.");
		TextPanel textPanel2 = new TextPanel("I am window 2.");
		TextPanel textPanel3 = new TextPanel("I am window 3.");
		TextPanel textPanel4 = new TextPanel("I am window 4.");
		TextPanel textPanel5 = new TextPanel("I am window 5.");

		// Create the dockables around the content components.
		Dockable dockable1 = new DefaultDockable("Window1", textPanel1, "Window 1", null, DockingMode.ALL);
		Dockable dockable2 = new DefaultDockable("Window2", textPanel2, "Window 2", null, DockingMode.ALL);
		Dockable dockable3 = new DefaultDockable("Window3", textPanel3, "Window 3", null, DockingMode.ALL);
		Dockable dockable4 = new DefaultDockable("Window4", textPanel4, "Window 4", null, DockingMode.ALL);
		Dockable dockable5 = new DefaultDockable("Window5", textPanel5, "Window 5", null, DockingMode.ALL);
		
		
		TabDock topTabDock = new TabDock();
		TabDock bottomTabDock = new TabDock();
		TabDock rightTabDock = new TabDock();

		// Add the dockables to these tab docks.
		topTabDock.addDockable(dockable1, new Position(0));
		topTabDock.addDockable(dockable2, new Position(1));
		bottomTabDock.addDockable(dockable3, new Position(0));
		rightTabDock.addDockable(dockable4, new Position(0));
		
		SplitDock topSplitDock = new SplitDock();
		topSplitDock.addChildDock(topTabDock, new Position(Position.CENTER));
		SplitDock bottomSplitDock = new SplitDock();
		bottomSplitDock.addChildDock(bottomTabDock, new Position(Position.CENTER));
		SplitDock rightSplitDock = new SplitDock();
		rightSplitDock.addChildDock(rightTabDock, new Position(Position.CENTER));

		
		FloatDockModel dockModel = new FloatDockModel();
		dockModel.addOwner("frame0", frame);
		DockingManager.setDockModel(dockModel);
		
		dockModel.addRootDock("topdock", topSplitDock, frame);
		dockModel.addRootDock("bottomdock", bottomSplitDock, frame);
		dockModel.addRootDock("rightdock", rightSplitDock, frame);
		
		leftSplitPane.setLeftComponent(topSplitDock);
		leftSplitPane.setRightComponent(bottomSplitDock);
		splitPane.setLeftComponent(leftSplitPane);
		splitPane.setRightComponent(rightSplitDock);

		// Dockable 5 should float. Add dockable 5 to the float dock of the dock model (
		// The float dock is a default root dock of the FloatDockModel.
		FloatDock floatDock = dockModel.getFloatDock(frame);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		floatDock.addDockable(dockable5, new Point(screenSize.width / 2, screenSize.height / 2), new Point());
		
	}

	public static final int FRAME_WIDTH = 600;
	public static final int FRAME_HEIGHT = 450;

	public static void createAndShowGUI() {

		// Create the frame.
		JFrame frame = new JFrame("First Example");

		// Create the panel and add it to the frame.
		TestDock panel = new TestDock(frame);
		frame.getContentPane().add(panel);

		// Set the frame properties and show it.
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation((screenSize.width - FRAME_WIDTH) / 2,
				(screenSize.height - FRAME_HEIGHT) / 2);
		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		frame.setVisible(true);

	}

	public static void main(String args[]) {
		Runnable doCreateAndShowGUI = new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		};
		SwingUtilities.invokeLater(doCreateAndShowGUI);

	}
	
	private class TextPanel extends JPanel
	{
		private JLabel label; 
		
		public TextPanel(String text)
		{
			super(new FlowLayout());
			
			// The panel.
			setMinimumSize(new Dimension(80,80));
			setPreferredSize(new Dimension(150,150));
			setBackground(Color.white);
			setBorder(BorderFactory.createLineBorder(Color.lightGray));
			
			// The label.
			label = new JLabel(text);
			label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
			add(label);
		}
	}
}
